auto.waitFor();

// console.show();
//设置屏幕的宽度，像素值
var widthG = device.width;
var widthParam = widthG / 1440;
//设置屏幕的高度，像素值
var heightG = device.height
var heightParam = heightG / 3120;
// setScreenMetrics(widthG, heightG);
var myFunc = require("./myCommonFunction.js");
// var myWord = require("./myWord.js");

var appPgName = getPackageName("支付宝");

var useWiFi = true;
//晚上8点到早上10点用WiFi
if (9 < hour && hour < 20) {
    useWiFi = false;
}
var doubleClick = true;
var now = new Date();
var hour = now.getHours();
var min = now.getMinutes();
myFunc.log(hour, min);

var storage = myFunc.initDateStorage();


var threadList = [];
var thread = null;
var thread2 = null;
var step = 1;

// get_alipay_points()
// xunluo()
// xunshi()
// liteTAsk();
// zuanYuanTask(0);
// senLinClick()
// log('end')
// exit();



var ret = myFunc.initNetWork(null, useWiFi);



var jifenNum = storage.get('支付宝积分')
thread3 = threads.start(function () {
    if ([10, 19, 22, 23].includes(hour) || jifenNum < 3) {
        sleep(780000)
    } else {
        sleep(300000)
    }
    myFunc.closeAppEx("支付宝");
    // img2.recycle();
    theadexit();
    exit();
});
threadList.push(thread3);


var thread4 = threads.start(function () {
    if (ret) {
        try {
            opreate();
        } catch (error) {
            myFunc.log(error);
        }
        myFunc.closeAppEx("支付宝");
        theadexit();
    }
    // img2.recycle();
    exit();
});
threadList.push(thread4);

function theadexit() {
    if (threadList.length > 0) {
        for (var i = 0; i < threadList.length; i++) {
            var thread = threadList[i];
            if (thread != null) {
                thread.interrupt();
            }
        }
    }
}

//程序主入口
function opreate() {

    //前置操作
    // prepareThings();
    //打开支付宝
    if (true || hour < 8) {
        openAlipay();
    } else {
        app.startActivity({
            action: "VIEW",
            data: "alipays://platformapi/startapp?appId=60000002"
        });
    }
    if (textEndsWith("排行榜").findOnce() != null && textEndsWith("查看更多动态").findOnce() == null) {
        back();
        sleep(500);
        scrollUp();
        sleep(500);
        scrollUp();
    }
    //判断是不是主号
    if (!storage.contains('主号')) {
        myFunc.clickByTextDesc("我的");
        var tt = '751';
        var a = textContains(tt).findOne(3000);
        if (a == null) {
            tt = '133';
        }
        if (textContains(tt).findOne(3000)) {
            storage.put('主号', 1);
            myFunc.clickByTextDesc("首页");
        } else {
            myFunc.clickDesc("设置");
            myFunc.clickTextByBtn("登录", 3000, 1);
            myFunc.textCpClick(tt);
            opreate();
            return;
        }
    }
    //进入蚂蚁森林主页,收集自己的能量
    collectMyself();

    myFunc.tuichuToStr('我的');
    log("到首页");
    if (hour > 20 && !storage.contains('巡护')) {
        xunluo();
    }
    mayi();
    log("返回到首页");

    sleep(1000);
    get_alipay_points();
    if (hour == 10 || hour == 19 || hour == 22 || hour == 23) {
        liteTAsk()
    }
    myFunc.log("退出");
    // if (!storage.contains('淘宝签到')) {
    //     qiandao();
    // }
}

function qiandao() {
    try {
        myFunc.openMyApp("淘宝");
        myFunc.log("签到");
        myFunc.clickDesc("签到");
        textContains("签到").findOne(5000);
        sleep(1000);

        renwu();
        // sleep(1000);
        // click(860, 420);
        myFunc.log("签到完");
        myFunc.tuichu("淘宝");
    } catch (error) {
        log(error);
        myFunc.tuichu("淘宝");
        qiandao();
    }
}

function renwu() {
    myFunc.findOne('第', 3000, true);
    sleep(1000);
    click('点击领取');
    sleep(1000);
    click('立即签到');
    sleep(1000);
    click(129, 1068);
    sleep(1000);
    var a = myFunc.findOnce('去逛逛');
    while (a != null) {
        a.click();
        textContains('滑动').findOne(5000);
        sleep(1000);
        for (let index = 0; index < 11; index++) {
            scrollDown();
            sleep(3000);
        }
        back();
        sleep(2000);
        click(129, 1068);
        sleep(1000);
        a = myFunc.findOnce('去逛逛');
    }
    if (click('去搜索')) {
        var a = desc('搜索发现').findOne(3000);
        sleep(500);
        click(a.bounds().right, a.bounds().bottom + 110);
        sleep(40000);
        myFunc.tuichuToStr('连续签到领元宝');
    }
    if (myFunc.findOnce('去下单') == null) {
        click(129, 1068);
        sleep(2000);
    }
    sleep(1000);
    if (click('去浏览')) {
        sleep(40000);
        myFunc.tuichuToStr('连续签到领元宝');
    }
    sleep(1000);
    if (myFunc.findOnce('去下单') == null) {
        click(129, 1068);
        sleep(2000);
    }
    if (myFunc.findOnce('明日再来') != null && myFunc.findOnce('去逛逛') == null
        && myFunc.findOnce('去下单') != null
        && myFunc.findOnce('点击领取') == null
        && myFunc.findOnce('去搜索') == null) {
        var now = new Date();
        var date = now.getDate();
        var month = now.getMonth();
        var storage = storages.create(month + '-' + date);
        var hour = now.getHours();
        var min = now.getMinutes();
        storage.put('淘宝签到', hour + '-' + min);
    }
    click('点击领取');
}


function mayi() {
    jin("蚂蚁庄园")
    sleep(6000);
    click(840, 2220);
    sleep(500);
    myFunc.tuichuToStr('扫一扫');
}

function xunluo() {
    jin("蚂蚁森林")
    sleep(1000);
    click(200, 1300);
    a = text("发现动物").findOne(5000);
    if (a == null) {
        app.startActivity({
            action: "VIEW",
            data: "alipays://platformapi/startapp?appId=68687842"
        });
    }
    sleep(1000);
    xunshi();
    var xun = textContains("开始巡护").findOne(5000);
    while (xun != null) {
        xun.click();
        sleep(4000);
        xunshi();
        if (textContains("兑换巡护机会").findOnce() != null) {
            break;
        }
        xun = textContains("开始巡护").findOne(1000);
    }
    xun = textContains("兑换巡护机会").findOne(5000);
    if (xun != null) {
        xun.click();
        sleep(500)
        if (textContains("步数不足").findOne(1000) != null || textContains("已达上限").findOne(1000) != null) {
            storage.put('巡护', '');
        } else {
            var dui = text("立即兑换").findOne(5000)
            var f = true;
            if (dui == null) {
                xunshi();
                textContains("兑换巡护机会").findOne(5000).click();
                if (textContains("步数不足").findOne(1000) != null || textContains("已达上限").findOne(1000) != null) {
                    storage.put('巡护', '');
                    f = false;
                } else {
                    text("立即兑换").findOne(5000).click();
                }
            } else {
                dui.click();
            }
            while (f) {
                var a = textContains("开始巡护").findOne(5000);
                if (a != null) {
                    a.click()
                    if (textContains("步数不足").findOne(1000) != null || textContains("去订阅").findOnce() != null) {
                        f = false;
                    } else {
                        sleep(4000);
                        xunshi();
                    }
                } else {
                    f = false;
                }
            }
        }
    }
    if (myFunc.findOnce('路线') != null && myFunc.findOnce('兑换') != null && myFunc.findOnce('步数不足', 1) != null) {
        log('巡护', myFunc.findOnce('步数不足', 1))
        storage.put('巡护', '');
    }
    sleep(500);
    myFunc.clickTextByBtn('图鉴')
    sleep(2000)
    var a = myFunc.find('熊猫树宝')
    myFunc.pClick(a[a.length - 1])
    myFunc.clickTextByBtn('派它巡护森林')
    myFunc.clickTextByBtn('确定')
    back();

}



function xunshi() {
    sleep(1000);
    // var answer1 = [ '风险是可控的', '青川箭竹', '另寻他路', '狠心不管', '不投食', '羊', '追寻踪迹', '5只', '用来驱逐野生动物',
    //     '亚洲金猫', '赶快离开', '找吃的', '猕猴桃', '后退', '盗猎者', '前面开路的', '争夺领地', '求偶', '鉴别熊猫个体', '在做标记', '不能带走', '仰鼻猴', '中小型动物', '定位用的', '辨认粪便'];
    //'立即守护'
    var answer1 = ['观看视频', '立即合成', '继续前进', '追寻踪迹', '定期回收', '关闭', '拼图奖励', '不会', '两年',
        '留下标记', '假的', '准备出发'];
    var a = className("android.widget.Button").boundsInside(50, 100, widthG, heightG).find();
    var len = a.length;
    var flag = true;
    var ii = 0
    while (flag && ii < 4) {
        sleep(1000);
        // if (len == 4) {
        //     a[3].click();
        //     sleep(800);
        // } else if (len == 6) {
        //     a[4].click();
        //     sleep(800);
        //     back();
        //     sleep(800);
        // } else if (len == 5) {
        //     var btUp = a[a.length - 2];
        //     var btDown = a[a.length - 1];
        //     var btUpText = btUp.text();
        //     log(btUpText, btDown.text());
        //     if (findAnswer(answer1, btUpText)) {
        //         btUp.click();
        //         log('click', btUpText);
        //     } else {
        //         btDown.click();
        //         log('click', btDown.text());
        //     }
        // } else {
        //     // len = 4 详情 路线 开始巡护 图鉴
        //     break;
        // }
        if (myFunc.findOnce('邀请TA') != null) {
            log('邀请ta')
            click(510, 1590)
            sleep(1000)
        } else {
            for (let index = 3; index < len; index++) {
                var bText = a[index].text()
                if (bText) {
                    log('text', bText)
                    if (findAnswer(answer1, bText)) {
                        log('点', bText)
                        a[index].click();
                        ii = 0;
                        if (bText.includes("观看视频")) {
                            sleep(3000);
                            back();
                        }
                        sleep(1000)
                        break;
                    } else if (index == len - 1) {
                        //没有答案
                        //1. 需要回答
                        if (len == 6) {
                            log('点最后一个答案', bText)
                            a[5].click();
                            break
                        } else if (len == 4) {
                            //2. 无需回答, 点开始巡护
                            if (!myFunc.clickByTextC('开始巡护')) {
                                flag = false;
                                break
                            }
                        }
                    }
                    if (bText.includes('兑换')) {
                        flag = false;
                        break
                    }

                }
            }
            ii++;
        }
        sleep(2000)
        a = className("android.widget.Button").boundsInside(50, 100, widthG, heightG).find();
        len = a.length;
        //点邀请的X
        // click(510, 1600)
    }

}
function findAnswer(answer, text) {
    for (var c of answer) {
        if (text.indexOf(c) != -1) {
            return true;
        }
    }
    return false;
}
//蚂蚁会员积分
function get_alipay_points() {
    step = 3
    openAlipay();
    var jifenNum = storage.get('支付宝积分')
    if (jifenNum >= 3) {
        if (hour >= 23) {
            storage.put('支付宝积分', 0);
        }
        return;
    }
    sleep(3000);
    myFunc.log("积分任务");
    myFunc.clickByText("我的");
    sleep(2000);

    // click(690 * widthParam, 780 * heightParam);
    if (!myFunc.clickByTextC("支付宝会员")) {
        click(544, 492)
    }

    // click(690 * widthParam, 710 * heightParam+100);
    sleep(3000);
    if (myFunc.clickByTextC("每日签到") && myFunc.findOne('去完成', 3000, 1) != null) {
        if (myFunc.findOne('去完成', 3000, 1) == null) {
            return
        }
        if (thread2 == null) {
            thread2 = threads.start(function () {
                log('过滑块');
                while (myFunc.findOne('下方滑块', 600000, 1) != null) {
                    myFunc.randomSwipe(170, 1538, 980, 1538);
                    sleep(2000);
                    if (myFunc.findOnce('点击框体', 1) != null) {
                        myFunc.clickByTextC('点击框体');
                        sleep(1000);
                    }
                }
            });
        }
        threadList.push(thread2);
        if (thread == null) {
            thread = threads.start(function () {
                while (1) {
                    if (myFunc.findOne('验证码拦截') != null) {
                        log('后退滑块');
                        back();
                        sleep(1000);
                    }
                }
            });
        }
        threadList.push(thread);
        sleep(8000);
        doJifenTask(jifenNum, 1);
    }

    // sleep(2000);
    // // myFunc.clickByTextDesc("签到领积分", 0);
    // back();
    // sleep(1000);
    // // var jia = textStartsWith("我的家").findOne(2000);
    // // if (jia != null) {
    // // jia.click();
    // click(1180 * widthParam, 205 * heightParam);
    // sleep(1500);
    // var ji = text("+1").findOne(2000);
    // if (ji != null) {
    //     myFunc.clickCenter(ji);
    //     storage.put('支付宝积分', '');
    // }

    // }

}

function doJifenTask(jifenNum, taskNum) {
    while (myFunc.findOnce('下方滑块', 1) != null) {
        myFunc.randomSwipe(170, 1538, 980, 1538);
        sleep(2000);
    }
    if (myFunc.findOnce('喵币', true) != null) {
        swipe(730, 1410, 750, 10, 500);
        sleep(1000);
    }
    sleep(1000);
    if (tuichuToStr('做任务赚积分', 500, undefined, () => get_alipay_points())) {
        return
    }
    var a = myFunc.findOnce('做任务赚积分');
    swipe(100, a.bounds().top, 100, 300, 1000);
    var tt = '';
    var list = null;
    var skipText = ['百度', '中国移动', '悟空浏览器', '皮皮虾', '淘特App', '点淘', '逛一逛闲鱼app', '视频', 
        '逛淘宝人生领新形象', '逛菜鸟', '淘宝特价版', '小组件', '玩', '今日头条'];

    var task15 = ['15', '滑动', '0元下单'];
    var taskKeys = ['+3积分', '+3', '+1积分', '+1'];
    for (let index = 0; index < taskKeys.length; index++) {
        let key = taskKeys[index];
        while ((list = myFunc.find(key)).length > 0) {
            log(key, "list len", list.length);
            var end = true;
            list = list.filter(x => !isListIncludes(skipText, x.text()));
            for (var a of list) {
                var tt = className('android.widget.TextView').boundsInside(0, a.bounds().top - 80, widthG, a.bounds().top).findOnce();
                if (tt == null) {
                    continue;
                }
                var t = tt.text();
                if (t.length == 0 || isListIncludes(skipText, t)) {
                    continue;
                }
                if (t.includes('15')) {
                } else {
                    skipText.push(t);
                }
                log(key, t);
                if (t == tt) {
                    continue;
                }
                end = false;
                tt = t;
                myFunc.clickCenter(a);
                sleep(1000);
                var q = text('去完成').boundsInside(100, 1400, 1000, 2000).findOne(2000);
                if (q != null) {
                    q.click();
                    index = -1;
                }

                if (isListIncludes(task15, t)) {
                    sleep(3000);
                    if (t.includes("搜索")) {
                        var sou = myFunc.findOne('猜你想搜');
                        if (sou == null) {
                            sou = myFunc.findOne('历史搜索');
                        }
                        if(sou != null) {
                            click(sou.bounds().centerX(), sou.bounds().bottom + 30);
                        }
                        sleep(2000);
                    }
                    for (let index = 0; index < 6; index++) {
                        swipe(500, 1500, 500, 500, 500);
                        sleep(3000);
                    }
                } else {
                    sleep(5000);
                }
                if (t.includes('3秒')) {
                    sleep(5000);
                } else {
                    huadong15s();
                }

                if (currentPackage() != getPackageName("支付宝")) {
                    myFunc.openMyApp('支付宝');
                }
                if (tuichuToStr('做任务赚积分', 500, undefined, () => get_alipay_points())) {
                    return
                }
                sleep(1000);
            }
            log(skipText);
            if (end) {
                break;
            }

        }
    }

    var word = ['蚂蚁森林', '蚂蚁庄园', '蚂蚁新村', '农场', '飞猪小程序', '支付宝运动', '花花卡', '淘金币'];
    for (var i = 0; i < word.length; i++) {
        var str = word[i];
        if (myFunc.clickByTextC(str, 500)) {
            log('浏览', str);
            sleep(1000);
            var q = text('去完成').boundsInside(100, 1400, 1000, 2000).findOne(2000);
            if (q != null) {
                q.click();
            }
            sleep(5000);
            huadong15s();
            if (currentPackage() != getPackageName("支付宝")) {
                myFunc.openMyApp('支付宝');
            } else if (str == '蚂蚁新村') {
                click(widthG / 2, heightG / 2);
            }
            if (tuichuToStr('做任务赚积分', 500, undefined, () => get_alipay_points())) {
                return
            }
            sleep(1000);
        }
    }
    sleep(2000);
    var a = myFunc.find('15秒', true);
    var smd = myFunc.find('15s', true);
    a = a.concat(smd);
    var endWords = ['赢积分', '得积分', '赚积分', '得 3 积分', '赚3积分', '得3积分', '得5积分'];
    //从a中过滤 赢积分 赚积分 得 3 积分
    a = a.filter(x => !isListIncludes(endWords, x.text()) && !isListIncludes(skipText, x.text()));
    while (a.length > 0) {
        for (var item of a) {
            myFunc.clickCenter(item);
            log('15秒 (', item.text());
            sleep(1000);
            var q = text('去完成').boundsInside(100, 1400, 1000, 2000).findOne(2000);
            if (q != null) {
                q.click();
            }
            sleep(3000);
            if (item.text().includes("搜")) {
                var sou = myFunc.findOne('猜你想搜');
                if(sou != null) {
                    click(sou.bounds().centerX(), sou.bounds().bottom + 30);
                }
                sleep(2000);
                huadong();
            } else if (myFunc.findOnce('逛一逛', 1) != null) {
                sleep(17000);
            } else {
                huadong();
            }

            if (currentPackage() != getPackageName("支付宝")) {
                myFunc.openMyApp('支付宝');
            }
            if (tuichuToStr('做任务赚积分', undefined, undefined, () => get_alipay_points())) {
                return
            }
            sleep(1000);
        }
        a = myFunc.find('15秒', true);
        a = a.filter(x => !isListIncludes(endWords, x.text()) && !isListIncludes(skipText, x.text()));
    }
    if (taskNum < 4) {
        myFunc.clickTextByBtn('换一换');
        sleep(2000);
        taskNum++;
        doJifenTask(jifenNum, taskNum);
        return;
    }
    if (myFunc.findOnce('已完成浏览任务') == null && myFunc.clickByTextC('逛一逛赚积分')) {
        log('逛15秒赚积分');
        sleep(2000);
        huadong();
    }
    a = myFunc.find('15秒', true);
    a = a.filter(x => !isListIncludes(endWords, x.text()) && !isListIncludes(skipText, x.text()));
    if (myFunc.findOnce('逛一逛赚积分', 1) != null && a.length == 0) {
        jifenNum = jifenNum + 1;
        storage.put('支付宝积分', jifenNum);
    }
}

function huadong15s() {
    if (myFunc.findOnce('做任务赚积分') == null &&
        myFunc.findOnce('逛一逛', 1) != null) {
        for (let index = 0; index < 6; index++) {
            swipe(500, 1500, 500, 500, 500);
            sleep(3000);
        }
    }
}

function isListIncludes(list, str) {
    return list.some(item => str.includes(item))
}

//获取权限和设置参数
function prepareThings() {
    //请求截图
    if (!requestScreenCapture()) {
        myFunc.log("请求截图失败,脚本退出");
        exit();
    }
    sleep(3000);
}


//获取截图
function getCaptureImg() {
    //captureScreen("/storage/emulated/0/DCIM/Screenshots/1.png");
    //sleep(500);
    var img0 = captureScreen();
    sleep(100);
    if (img0 == null || typeof (img0) == "undifined") {
        myFunc.log("截图失败,脚本退出");
        exit();
    } else {
        return img0;
    }
}


//从支付宝主页进入蚂蚁森林我的主页
function collectMyself() {
    sleep(1000);
    var a = text("蚂蚁森林").findOne(5000);
    var x = 1;
    while (a == null && x < 6) {
        openAlipay();
        sleep(1000);
        a = text("蚂蚁森林").findOne(5000);
        x++;
    }
    myFunc.pClick(a);
    text("种树").findOne(3000);
    var i = 1;
    while (!textEndsWith("蚂蚁森林").exists() && !descEndsWith("蚂蚁森林").exists() && i <= 5) {
        sleep(1000);
        i++;
    }
    myFunc.log("我的主页");
    sleep(1000);
    if (myFunc.findOnce('蚂蚁森林') == null && textContains('排行榜').findOne(3000) == null) {
        myFunc.tuichu("支付宝");
        opreate();
        return;
    }
    // if(hour == 7 && min < 30) {
    //     images.captureScreen("./蚂蚁/" + now.getDate() + "-" + hour + "-" + min + ".png");
    // }
    // var days = now.getDay();
    // if (0 != days && 1 != days) {
    // if (0 != days) {
    if (hour == 7 && min <= 10 && min > 5 || hour == 19) {
        // shuangjika();
    }
    if (hour == 7 && min <= 18 && min > 5 || hour == 19) {
        // doubleClick = true;
    }
    // }
    // if (0 != days && 6 != days && 7 != days) {
    // if (hour == 13) {
    // nengliangzhao();
    // }
    // }

    //收自己能量
    collectNergy();
    return true;
}

function nengliangzhao() {
    sleep(1000);
    click(290 * widthParam, 2078 * heightParam);
    sleep(800);

    var comBox = text("限时保护罩").findOnce();
    if (comBox == null) {
        comBox = text("能量保护罩").findOnce();
    }
    if (comBox != null) {
        let x = comBox.bounds().centerX();
        let y = comBox.bounds().centerY();
        click(x, y + 130);
        sleep(800);
        text("立即使用").findOne(3000).click();
        sleep(800);
        try {
            text("确认").findOne(1000).click();
            text("关闭").findOne(1000).click();

        } catch (error) {

        }
    }

}

function shuangjika() {
    log("双击卡");
    // if (storage.contains('救能量')) {
    // return;
    // }
    sleep(2000);
    //点背包
    click(245, 1595);
    sleep(1000);

    //点兑换
    // click(200, 1595);
    // sleep(1000);
    // myFunc.findOne('兑换');
    // sleep(1000);
    // var ka = myFunc.findOne('能量双击卡');
    // if (ka != null) {
    //     ka.click();
    //     sleep(2000);
    //     myFunc.clickByText('立即兑换');
    //     sleep(800);
    //     myFunc.clickByText('确认兑换');
    // }
    // myFunc.tuichuToStr('蚂蚁森林')

    sleep(1000);
    var comBox = text("限时双击卡").findOnce();
    if (comBox == null) {
        comBox = text("能量双击卡").findOnce();
    }
    let x = comBox.bounds().centerX();
    let y = comBox.bounds().centerY();
    click(x, y + 130);
    sleep(800);
    try {
        text("立即使用").findOne(3000).click();
    } catch (e) {

    }
    doubleClick = true;
    // storage.put('救能量', '')
}


function collectNergy() {
    collect1();
}

function collect1() {
    while (scrollUp()) {

    }
    //收自己的能量
    //圆心
    var rx = widthG / 2;
    var ry = heightG / 11 * 6;
    // myFunc.log(rx, ry)
    //半径
    // var r = 845;
    var r = 700;
    //收能量
    for (let hudu = 50; hudu < 131; hudu = hudu + 10) {
        let jiaodu = hudu * Math.PI / 180;
        var rrx = rx - Math.cos(jiaodu) * r;
        var rry = ry - Math.sin(jiaodu) * r;
        click(rrx, rry)
        // sleep(300);
    }
    sleep(500)
    if (myFunc.findOne('还可分享', 1000, 1) != null) {
        myFunc.tuichuToStr('扫一扫');
        sleep(800);
        myFunc.textpClick('蚂蚁森林');
        myFunc.findOne('森林广场', 8000);
        collect1();
        return
    }
    //点下一个
    var y = 0.66 * heightG;
    y = 1574
    var x = widthG - 50;
    click(x, y);
    var end = textStartsWith("返回我的森林").findOne(3000);
    if (end != null) {
        return
    }
    var rrx = widthG / 2;
    var rry = 1380;
    while (1) {
        //收能量
        click(rrx, rry)
        if (doubleClick) {
            sleep(1000)
            click(rrx, rry)
        }
        sleep(500)
        //点下一个
        click(x, y);
        var end = textStartsWith("返回我的森林").findOne(3000);
        if (end != null) {
            myFunc.log("收完")
            break;
        }
    }
}


function openAlipay() {
    if (appPgName != currentPackage()) {
        myFunc.openMyApp("支付宝");
    } else {
        if (textContains("首页").exists()) {
            myFunc.clickText("首页");
            sleep(800)
        }
        if (!textContains('收付款').exists()) {
            myFunc.tuichuToStr('收付款')
        }
    }
}

function jin(menu) {
    log('进', menu)
    var a = text(menu).findOne(3000);
    var x = 1;
    while (a == null && x < 6) {
        myFunc.log("重进" + menu);
        openAlipay();
        sleep(1000);
        a = text(menu).findOne(3000);
        x++;
    }
    myFunc.pClick(a);
}
function liteTAsk() {
    log('开始 liteTAsk')
    openAlipay()
    openAlipay()
    //蚂蚁森林任务
    senLinClick();
    myFunc.tuichuToStr("收付款");
    //做蚂蚁庄园任务
    zuanYuanTask();
    if (hour < 19) {
        return
    }
    //换小号
    log("换小号");
    myFunc.tuichuToStr('收付款');
    myFunc.clickByText('我的')
    sleep(1500);
    click(1000, 188);
    sleep(1500);
    click(575, 2000);
    sleep(1000);
    if (myFunc.findOnce('管理名下账号') == null) {
        click(575, 1740);
        sleep(1000);
    }
    myFunc.clickByTextC('199', 1000)
    //进入小号
    myFunc.log("进入小号");
    //蚂蚁森林任务
    senLinClick(1);

    myFunc.tuichuToStr("收付款");
    //做蚂蚁庄园任务
    zuanYuanTask(1)

    //换大号
    myFunc.log("换大号");
    myFunc.tuichuToStr('收付款');
    myFunc.clickByText('我的')
    sleep(1500);
    click(1000, 188);
    sleep(1500);
    click(575, 1800);
    sleep(1000);
    if (myFunc.findOnce('管理名下账号') == null) {
        click(575, 1740);
        sleep(1000);
    }
    myFunc.clickByTextC('133', 1000)
    senLinClick(0);

    click('领取')
    myFunc.tuichuToStr('收付款');
    jin("蚂蚁庄园");
    sleep(4000);
    // click(490, 2228);
    click(303, 2228);
    myFunc.findOne("今天");
    sleep(1000);
    for (var iterator of myFunc.find('g领取', 1)) {
        iterator.click()
        sleep(1000);
    }
    for (var iterator of myFunc.find('立即领取')) {
        iterator.click()
        sleep(1000);
    }
}

function senLinClick(xiaohao) {
    jin("蚂蚁森林");
    text("收好友").findOne(3000);
    if (xiaohao) {
        //去浇水
        scrollDown();
        sleep(800)
        myFunc.clickTextByBtn('遥梦', 1000, 1);
        sleep(3000)
        for (let index = 0; index < 4; index++) {
            click(1000, 1848)
            sleep(800)
            click(1000, 2300)
            sleep(1000)
        }
        if (tuichuToStr('去保护', undefined, undefined, () => senLinClick(xiaohao))) {
            return
        }
        sleep(1000);
        scrollUp()
        sleep(1000);
    }
    sleep(1000);
    click(380, 1600);
    sleep(2000);
    log('打开任务')
    if (myFunc.findOne('今天') == null) {
        while (scrollUp()) { }
        sleep(500);
        click(380, 1600);
        sleep(1000);
    }
    sleep(2000);
    if (myFunc.clickTextByBtn("去拯救", 1000)) {
        log('去拯救');
        sleep(2000);
        if (myFunc.findOnce('返回蚂蚁森林', 1) != null
            || myFunc.findOnce('再来一次', 1) != null) {
            engines.execScriptFile('救能量.js');
            sleep(15000);
            textContains("蚂蚁").waitFor();
            sleep(2000);
            var a = myFunc.findOnce('顺利');
            if (xiaohao) {
                a = myFunc.findOnce('遥梦溪谈');
            }
            if (a != null) {
                click(a.bounds().centerX(), a.bounds().bottom + 70);
                sleep(500);
                if (myFunc.findOnce('返回蚂蚁森林', 1) != null) {
                    engines.execScriptFile('救能量.js');
                    sleep(15000);
                    textContains("蚂蚁").waitFor();
                }
            }
        }
        if (tuichuToStr('今天', undefined, undefined, () => senLinClick(xiaohao))) {
            return
        }
        sleep(1000);
    }
    if (!xiaohao) {
        //已经做过的任务
        var endWords = []
        for (var i = 0; i < 3; i++) {
            sleep(1000);
            var oneClickWord = ['光伏治沙', '活力值新攻略', '真爱合种', '逛百县'];
            otherTask(oneClickWord, '去完成', endWords);

            var oneClickWord = ['双击卡上新', '逛森林集市', '活力值新攻略', '许个愿', '旧鞋', 
                '查看', '看看', '外卖券'];
            otherTask(oneClickWord, '去看看', endWords);

            var oneClickWord = ['去快手', '去淘金币', '淘宝人生', '芝麻攒粒', '绿色租赁', '美团视频', '闲鱼红包', '天猫领福气'];
            otherTask(oneClickWord, '去看看', endWords, 7000);

            var oneClickWord = ['厦马碳中和'];
            otherTask(oneClickWord, '去助力', endWords);
            swipe(100, 2000, 100, 200, 1000)
        }

    }

    function otherTask(oneClickWord, doStr, endWords, sleepTime) {
        sleepTime = sleepTime || 1000;
        for (var cWord of oneClickWord) {
            if (endWords.includes(cWord)) {
                continue;
            }
            var oneClick = myFunc.findOnce(cWord, 1);
            if (oneClick == null) {
                continue;
            }
            var btn = className('android.view.View').boundsInside(widthG / 2, oneClick.bounds().top - 10, device.width, oneClick.bounds().bottom + 10).findOnce();
            if (btn != null) {
                let x = btn.bounds().centerX();
                let y = btn.bounds().centerY();
                if (x < 1 || y < 1) {
                    continue;
                }
                myFunc.clickCenter(btn);
                endWords.push(cWord);
                sleep(sleepTime);
                if (myFunc.clickByText('知道了', 1000)) {
                    continue;
                }
                checkAndOpenAlipay()
                myFunc.tuichuToStr('玩一场能量雨', 1000, 1);
                sleep(1000);
                click('立即领取');
                sleep(2000);
            }
            sleep(1000);
        }
    }

    if (!xiaohao) {
        while (myFunc.clickTextByBtn("去转化", 1000)) {
            log('去转化');
            sleep(2000);
            click(950, 1690);
            sleep(1000);
            click(967, 1928);
            myFunc.findOne('去完成');
            sleep(800);
            click('领取');
            sleep(1000);
            if (tuichuToStr(["今天", "玩一场能量雨"], 1000, 1, () => senLinClick(xiaohao))) {
                return
            }
        }
    }
    myFunc.clickTextByBtn("去打卡", 1000);
    sleep(800);

    click('立即领取');
}


function zuanYuanTask(xiaohao) {
    step = 2;
    jin("蚂蚁庄园");
    sleep(4000);
    //送饲料
    if (xiaohao) {
        click(543, 1459);
        myFunc.findOne('遥梦溪谈的蚂蚁庄园', 5000);
        sleep(2000);
        //种麦子
        for (let index = 0; index < 4; index++) {
            click(300, 2250);
            sleep(1000);
            if (!myFunc.clickByText('确认')) {
                break;
            }
            sleep(2000);
        }
        back();

        if (myFunc.findOnce('收付款') != null) {
            zuanYuanTask(xiaohao)
            return
        }
    }
    //领饲料
    click(303, 2228);
    for (let index = 0; index < 3; index++) {
        if (myFunc.findOne("今天", 2000) == null) {
            click(303, 2228);
            sleep(2000);
        } else {
            break;
        }
    }
    sleep(800);
    var word = myFunc.findOne('庄园小课堂', 3000, 1);
    var btnList = className('android.widget.Button').indexInParent(0).depth(word.depth()).find();
    // var taskList = className('android.widget.TextView').indexInParent(0).depth(word.depth()).find();
    // var cha = btnList.length - taskList.length;
    // log(btnList.length, taskList.length);
    if (word.text().includes('去答题') && !xiaohao) {
        word.click();
        log('小课堂');
        myFunc.findOne('题目来源', 3000, 1);
        sleep(800);
        var questions = className('android.widget.TextView').indexInParent(0).depth(17).find();
        var result = questions.map(question => question.text()).join("");
        if (result) {
            var answer1 = className('android.widget.TextView').indexInParent(0).depth(18).findOnce();
            var answer2 = className('android.widget.TextView').indexInParent(1).depth(18).findOnce();
            var data = {
                "model": 'deepseek-v3',
                "messages": [
                    {
                        "role": "system",
                        "content": "不用解释,直接输出正确答案, 并把答案用[]包裹, ex:`1+3=?;答案:[4]`"
                    },
                    {
                        "role": 'user',
                        "content": result + "\n" + "1:" + answer1.text() + "\n" + "2:" + answer2.text()
                    }
                ]
            }
            var options = {
                method: 'POST',
                headers: {
                    Authorization: 'Bearer sk-kQKMTKyEQA7X6eZ_VqPVG2cjQ1pVmmZIcJ0Mk_dpHbpqmPwtcGDorwmDSxU',
                    'content-type': 'application/json'
                },
                body: JSON.stringify(data)
            };
            var res = http.request('https://fe6638332761e70f05a9fd36163e80d5c3564100-3000.dstack-prod5.phala.network/v1/chat/completions', options);
            if (res.statusCode == 200) {
                var a = res.body.json();
                var ans = a.choices[0].message.content;
                log('outAnswer', ans);
                // 使用正则表达式提取方括号中的内容
                var outAnswer = ans.match(/\[(.*?)\]/);
                if (outAnswer) {
                    outAnswer = outAnswer[1];
                    if (outAnswer == "1" || outAnswer == answer1.text()) {
                        myFunc.clickCenter(answer1);
                    } else if (outAnswer == "2" || outAnswer == answer2.text()) {
                        myFunc.clickCenter(answer2);
                    }
                } else {
                    log("未能从AI回答中解析出答案");
                }
            }
        }
        sleep(1000);
        if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
            return
        }
    }
    for (var i = 0; i < btnList.length; i++) {
        // log(taskList[i].text(), btnList[i + cha].text());
        // log(btnList[i].text());
        if (btnList[i].text().includes('去完成')) {
            var taskName = btnList[i].text();
            var clickTask = ['逛一逛蚂蚁新村', '逛一逛余额宝会场', '去快手逛一逛', '神奇海洋']
            var guohuakuaiTask = ['去逛一逛淘宝芭芭农场', '去淘宝签到逛一逛']
            if (taskName.includes('小鸡家庭')) {
                btnList[i].click();
                log('小鸡家庭');
                myFunc.findOne('历史累计亲密度', 10000, 1);
                sleep(2000);
                //点任务
                click(528, 2252);
                sleep(2000);
                let needDo = true;
                while (needDo) {
                    if (xiaohao && myFunc.clickByText('去喂食', 1000)) {
                        log('去喂食');
                        sleep(1000);
                        myFunc.clickByTextC('确认')
                    } else if (xiaohao && myFunc.clickByText('去请客', 1000)) {
                        log('去请客');
                        sleep(1000);
                        myFunc.clickByTextC('确认')
                    } else if (myFunc.clickTextByBtn('去指派', 1000)) {
                        log('去指派');
                        sleep(1000);
                        myFunc.clickByText('确认', 1000)
                        sleep(2000);
                        //点任务
                        click(528, 2252);
                        sleep(2000);
                    } else if (xiaohao && myFunc.clickTextByBtn('去捐步', 1000)) {
                        log('去捐步');
                        sleep(1000);
                        myFunc.clickByText('去捐步数')
                        myFunc.clickByText('立即捐步')
                        if (tuichuToStr('相亲相爱一家人', 1000, 1, () => zuanYuanTask(xiaohao))) {
                            return
                        }
                        sleep(2000);
                        //点任务
                        click(528, 2252);
                        sleep(2000);
                    } else if (myFunc.clickTextByBtn('去捐蛋', 1000)) {
                        log('去捐蛋');
                        sleep(1000);
                        myFunc.clickByText('去捐蛋')
                        myFunc.clickByText('立即捐蛋')
                        sleep(1000);
                        myFunc.clickByText('立即捐蛋')
                        sleep(1000);
                        if (tuichuToStr('相亲相爱一家人', 1000, 1, () => zuanYuanTask(xiaohao))) {
                            return
                        }
                        if (myFunc.findOnce('捐蛋反馈') != null) {
                            click(1000, 780)
                            sleep(1000);
                        }
                    } else {
                        needDo = false;
                    }
                }
                if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
                    return
                }
                if (xiaohao) {
                    click('领取');
                    break;
                }
            }
            if (!xiaohao) {
                if (taskName.includes('庄园小视频')) {
                    log('小视频');
                    btnList[i].click();
                    sleep(20000);
                    if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
                        return
                    }
                } else if (taskName.includes('小鸡厨房')) {
                    btnList[i].click();
                    log('小鸡厨房');
                    sleep(4000);
                    click(950, 1750);
                    sleep(2000);
                    click(825, 2236);
                    if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
                        return
                    }
                } else if (isListIncludes(guohuakuaiTask, taskName)) {
                    btnList[i].click();
                    log(taskName);
                    guohuakuai();
                    checkAndOpenAlipay();
                    if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
                        return
                    }
                } else if (taskName.includes('去杂货铺逛一逛')) {
                    btnList[i].click();
                    log('杂货铺逛一逛');
                    guohuakuai();
                    huadong();
                    if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
                        return
                    }
                } else if (isListIncludes(clickTask, taskName)) {
                    btnList[i].click();
                    log(taskName);
                    sleep(2000);
                    // 对于快手任务，需要检查并打开支付宝
                    if (taskName.includes('去快手逛一逛')) {
                        checkAndOpenAlipay();
                    }
                    if (tuichuToStr('庄园小课堂', 1000, 1, () => zuanYuanTask(xiaohao))) {
                        return
                    }
                } else if (taskName.includes('抽抽乐')) {
                    btnList[i].click()
                    log('抽抽乐');
                    sleep(3000);
                    myFunc.findOne('每日签到', 5000, 1)
                    while(myFunc.findOne('完成', 3000, 1) == null) {
                        myFunc.tuichuToStr('庄园小课堂', 1000, 1);
                        sleep(1000)
                        myFunc.clickCenter(btnList[i])
                        sleep(3000);
                        myFunc.findOne('每日签到', 5000, 1)
                    }
                    click('领取')
                    sleep(2000);
                    var isEnough = 0;
                    var wancBtnList = myFunc.find('去完成');
                    log('去完成-len', wancBtnList.length);
                    var wancBtnIdx = 0;
                    while (wancBtnList.length > 0 && wancBtnIdx < wancBtnList.length) {
                        log('去完成', wancBtnIdx);
                        var wancBtn = wancBtnList[wancBtnIdx];
                        myFunc.clickCenter(wancBtn)
                        sleep(2000);
                        if (myFunc.findOnce('确认兑换') != null && isEnough < 3) {
                            log('兑换', wancBtnIdx)
                            myFunc.clickByText('确认兑换');
                            isEnough++;
                            wancBtnIdx = 0;
                        } else if (myFunc.findOnce('滑动浏览', 1) != null) {
                            log('滑动浏览', wancBtnIdx)
                            guohuakuai();
                            huadong();
                            if (tuichuToStrByOr(['抽抽乐', '每日签到', '已完成'], 1000, 1, () => zuanYuanTask(xiaohao))) {
                                return
                            }
                            if (tuichuToStr('领取', undefined, undefined, () => zuanYuanTask(xiaohao))) {
                                return
                            }
                            wancBtnIdx = 0;
                        } else if (myFunc.findOnce('数字藏品', 1) != null) {
                            log('数字藏品', wancBtnIdx)
                            sleep(1000);
                            if (tuichuToStrByOr(['抽抽乐', '每日签到', '已完成'], 1000, 1, () => zuanYuanTask(xiaohao))) {
                                return
                            }
                            wancBtnIdx = 0;
                            // myFunc.tuichuToStr('领取')
                        } else if (myFunc.findOnce('IP抽抽乐', 1) != null) {
                            log('IP抽抽乐', wancBtnIdx)
                            sleep(1000);
                            IP抽抽乐(xiaohao);
                            sleep(1000);
                            wancBtnIdx = 0;
                            // myFunc.tuichuToStr('领取')
                            back();
                        } else {
                            log('其它', wancBtnIdx)
                            wancBtnIdx += 1;
                            if (tuichuToStrByOr(['抽抽乐', '每日签到', '已完成'], 1000, 1, () => zuanYuanTask(xiaohao))) {
                                return
                            }
                            break
                        }
                        sleep(1000);
                        click('领取')
                        sleep(2000);
                        wancBtnList = myFunc.find('去完成');
                        if (wancBtnList.length == 0) {
                            log('完成所有任务')
                            break
                        }
                    }
                    click('领取')
                    sleep(2500);
                    var q = myFunc.findOne('次机会', 3000, 1);
                    var qNum = -1;
                    if (q == null) {
                        myFunc.tuichuToStr('庄园小课堂', 1000, 1);
                        myFunc.clickTextByBtn('抽抽乐', 3000, 1)
                        sleep(3000);

                    } else {
                        //使用正则从q.text()中获取次数
                        var matchResult = q.text().match(/\d+/);
                        qNum = matchResult ? parseInt(matchResult[0], 10) : -1; // 或者设置一个默认值
                        if (qNum < 0) {
                            if (q.parent() != null) {
                                q = q.parent().children()
                                qNum = parseInt(q[1].text());
                                q = q[0];
                            }
                        }
                    }

                    if (qNum > 0) {
                        var zhidao = true;
                        log('抽奖', qNum)
                        for (let index = 0; index < qNum; index++) {
                            myFunc.clickCenter(q);
                            // myFunc.tuichuToStr('庄园小课堂', 1000, 1);
                            // myFunc.clickByTextC('庄园小课堂')
                            // myFunc.findOne('每日签到', 3000, 1)
                            sleep(3000);
                            if (zhidao) {
                                if (!myFunc.clickByTextC('知道')) {
                                    zhidao = false;
                                }
                            }
                            if (!zhidao) {
                                myFunc.clickCenter(q);
                            }
                            sleep(1000);
                        }
                        myFunc.tuichuToStr('蚂蚁森林');
                        jin('蚂蚁庄园');
                        sleep(1000);
                        for (let index = 0; index < qNum * 2; index++) {
                            sleep(1000);
                            click(1273 * widthParam, 2880 * heightParam);
                            sleep(500);
                        }
                        click(303, 2228);
                        sleep(1000);
                    } else {
                        log('没有获取到次数qNum')
                    }
                    //myFunc.tuichuToStr('庄园小课堂', 1000, 1);

                }
            }



        }

    }
}

function checkAndOpenAlipay() {
    if (currentPackage() != getPackageName("支付宝")) {
        myFunc.openMyApp('支付宝');
    }
}
function IP抽抽乐(xiaohao) {
    sleep(1000);
    click('领取')
    sleep(2500);
    var q = myFunc.findOne('次机会', 3000, 1);
    if (q != null) {
        myFunc.clickCenter(q);
    } else {
        log('没有获取到次数1')
    }
}

function guohuakuai() {
    sleep(8000);
    var ii = 0;
    while (myFunc.findOnce('下方滑块', 1) != null && ii < 6) {
        myFunc.randomSwipe(170, 1538, 980, 1538)
        sleep(2000)
        if (myFunc.findOnce('点击框体', 1) != null) {
            myFunc.clickByTextC('点击框体');
            sleep(1000)
        }
        if (ii == 5) {
            break
        }
    }
}

function huadong() {
    if (myFunc.findOnce('立即领取') != null) {
        click(533, 1710);
        sleep(800);
    }
    for (let index = 0; index < 6; index++) {
        swipe(500, 1500, 500, 500, 500)
        sleep(1500);
        swipe(500, 500, 500, 1500, 500)
        sleep(1500);
    }
}



function tuichuToStr(searchTexts, retryDelay, useFuzzy, prematureExitCallback) {
    retryDelay = (retryDelay === undefined) ? 1000 : retryDelay;
    // 如果 useFuzzy 未传入或传入 undefined，则设置为 false
    useFuzzy = (useFuzzy === undefined) ? false : useFuzzy;

    // 将输入统一处理为数组，便于后续遍历
    const textsToFind = Array.isArray(searchTexts) ? searchTexts : [searchTexts];
    const MAX_ATTEMPTS = 8; // 最多尝试次数 (原逻辑 idx > 10 对应 0 到 10 共 11 次)
    let foundText = false; // 标记是否找到了任何一个目标文本
    let didPrematureExit = false; // 标记是否因为特殊条件提前退出
    // 2. 生成符合需求的代码示例 (主体循环)
    for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
        log(`开始第 ${attempt + 1} 次尝试查找...`);
        foundText = true; // 重置查找状态
        // 2.1 内部循环：查找当前屏幕上的文本
        for (var word of textsToFind) {
            let widget = null;
            widget = useFuzzy
                ? textContains(word).findOne(800) // 模糊查找
                : text(word).findOne(800);        // 精确查找
            if (widget === null) {
                foundText = false;
                break;
            }
        }
        // 2.2 判断查找结果
        if (foundText) {
            break; // 找到文本，退出外层尝试循环
        }
        // 2.3 如果未找到，并且还未达到最大尝试次数，则执行返回、等待、检查特殊退出条件
        if (attempt < MAX_ATTEMPTS - 1) {
            try {
                back(); // 执行返回操作
                sleep(retryDelay); // 等待指定时间
                // 2.4 检查特殊退出条件
                let foundPaymentText = myFunc.findOnce('收付款') !== null;
                let isLauncherActivity = currentActivity() === 'com.microsoft.launcher.LauncherActivity'; // 假设 currentActivity 全局可用
                if (foundPaymentText || isLauncherActivity) {
                    log('到首页/桌面');
                    didPrematureExit = true;
                    // 3. 提供代码解释 & 最佳实践建议 (回调检查)
                    if (typeof prematureExitCallback === 'function') {
                        try {
                            log('执行回调函数', prematureExitCallback);
                            prematureExitCallback();
                        } catch (e) {
                            log(`执行回调函数时出错: ${e}`);
                        }
                    } else if (prematureExitCallback !== undefined) {
                        // 如果提供了参数但不是函数，给出提示可能有助于调试
                        log('警告：提供了 prematureExitCallback 参数，但它不是一个函数。');
                    }
                    break; // 满足特殊条件，退出外层尝试循环
                }
            } catch (e) {
                log(`在执行 back(), sleep() 或检查退出条件时发生错误: ${e}`);
                // 发生错误时，可能需要决定是继续还是中断，这里选择中断以避免潜在的无限循环或意外行为
                didPrematureExit = false; // 明确未因预期条件退出
                break;
            }
        } else {
            // 已达到最大尝试次数但仍未找到
            log(`已达到最大尝试次数 (${MAX_ATTEMPTS})，未找到目标文本。`);
            // 2.4 检查特殊退出条件
            let foundPaymentText = myFunc.findOnce('收付款') !== null;
            let isLauncherActivity = currentActivity() === 'com.microsoft.launcher.LauncherActivity'; // 假设 currentActivity 全局可用
            if (foundPaymentText || isLauncherActivity) {
                log('到首页/桌面');
                didPrematureExit = true;
                // 3. 提供代码解释 & 最佳实践建议 (回调检查)
                if (typeof prematureExitCallback === 'function') {
                    try {
                        log('执行回调函数', prematureExitCallback);
                        prematureExitCallback();
                    } catch (e) {
                        log(`执行回调函数时出错: ${e}`);
                    }
                } else if (prematureExitCallback !== undefined) {
                    // 如果提供了参数但不是函数，给出提示可能有助于调试
                    log('警告：提供了 prematureExitCallback 参数，但它不是一个函数。');
                }
                break; // 满足特殊条件，退出外层尝试循环
            }

        }
    }
    // 返回是否因为特殊条件而提前退出，与原函数行为保持一致
    return didPrematureExit;
}

function tuichuToStrByOr(searchTexts, retryDelay, useFuzzy, prematureExitCallback) {
    retryDelay = (retryDelay === undefined) ? 1000 : retryDelay;
    // 如果 useFuzzy 未传入或传入 undefined，则设置为 false
    useFuzzy = (useFuzzy === undefined) ? false : useFuzzy;

    // 将输入统一处理为数组，便于后续遍历
    const textsToFind = Array.isArray(searchTexts) ? searchTexts : [searchTexts];
    const MAX_ATTEMPTS = 8; // 最多尝试次数 (原逻辑 idx > 10 对应 0 到 10 共 11 次)
    let foundText = false; // 标记是否找到了任何一个目标文本
    let didPrematureExit = false; // 标记是否因为特殊条件提前退出
    // 2. 生成符合需求的代码示例 (主体循环)
    for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
        log(`开始第 ${attempt + 1} 次尝试查找...`);
        foundText = false; // 重置查找状态
        // 2.1 内部循环：查找当前屏幕上的文本
        for (var word of textsToFind) {
            let widget = null;
            widget = useFuzzy
                ? textContains(word).findOne(800) // 模糊查找
                : text(word).findOne(800);        // 精确查找
            if (widget !== null) {
                log(`找到: "${word}"`);
                foundText = true;
                break; // 找到一个即退出内层循环
            }
        }
        // 2.2 判断查找结果
        if (foundText) {
            break; // 找到文本，退出外层尝试循环
        }
        // 2.3 如果未找到，并且还未达到最大尝试次数，则执行返回、等待、检查特殊退出条件
        if (attempt < MAX_ATTEMPTS - 1) {
            log(`第 ${attempt + 1} 次尝试未找到任何目标文本。`);
            try {
                back(); // 执行返回操作
                sleep(retryDelay); // 等待指定时间
                // 2.4 检查特殊退出条件
                const foundPaymentText = myFunc.findOnce('收付款') !== null; // 假设 myFunc 全局可用
                const isLauncherActivity = currentActivity() === 'com.microsoft.launcher.LauncherActivity'; // 假设 currentActivity 全局可用
                if (foundPaymentText || isLauncherActivity) {
                    log('到首页/桌面');
                    didPrematureExit = true;
                    // 3. 提供代码解释 & 最佳实践建议 (回调检查)
                    if (typeof prematureExitCallback === 'function') {
                        try {
                            log('执行回调函数', prematureExitCallback);
                            prematureExitCallback();
                        } catch (e) {
                            log(`执行回调函数时出错: ${e}`);
                        }
                    } else if (prematureExitCallback !== undefined) {
                        // 如果提供了参数但不是函数，给出提示可能有助于调试
                        log('警告：提供了 prematureExitCallback 参数，但它不是一个函数。');
                    }
                    break; // 满足特殊条件，退出外层尝试循环
                }
            } catch (e) {
                log(`在执行 back(), sleep() 或检查退出条件时发生错误: ${e}`);
                // 发生错误时，可能需要决定是继续还是中断，这里选择中断以避免潜在的无限循环或意外行为
                didPrematureExit = false; // 明确未因预期条件退出
                break;
            }
        } else {
            // 已达到最大尝试次数但仍未找到
            log(`已达到最大尝试次数 (${MAX_ATTEMPTS})，未找到目标文本。`);
        }
    }
    // 返回是否因为特殊条件而提前退出，与原函数行为保持一致
    return didPrematureExit;
}